package com.schoolcloud.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.schoolcloud.system.dto.UserAuthRoleDTO;
import com.schoolcloud.system.entity.SysUserRole;
import com.schoolcloud.system.mapper.SysUserRoleMapper;
import com.schoolcloud.system.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hongmizfb
* @description 针对表【sys_user_role】的数据库操作Service实现
* @createDate 2024-10-19 23:07:20
*/
@Service
@RequiredArgsConstructor
public class SysUserRoleServiceImpl implements SysUserRoleService{

    private final SysUserRoleMapper userRoleMapper;
    @Override
    public void addUserRole(UserAuthRoleDTO dto) {
        List<Integer> roleIds = dto.getRoleIds();
        if (CollUtil.isNotEmpty(roleIds)){
            for (Integer roleId : roleIds) {
                SysUserRole userAuthRole = new SysUserRole();
                userAuthRole.setRoleId(roleId);
                userAuthRole.setUserId(dto.getUserId());
                userRoleMapper.addUserRole(userAuthRole);
            }
        }
    }

    @Override
    public List<Integer> getUserRoleByUserId(Integer userId) {
        return userRoleMapper.getUserRoleByUserId(userId);
    }
}
